var jqGrid = {
	index : -1,
	grid : '#jqGrid',
	pager : '#jqGridPager',
	init : function(opt, grid, pager) {
		jqGrid.grid = grid || jqGrid.grid;
		jqGrid.pager = pager || jqGrid.pager;
		// 表格配置
		opt = $.extend({
			url : "list.json",
			editurl : "save.json",
			responsive : true,
			mtype : "POST",
			datatype : "json",
			height : 250,
			rowNum : 20,
			styleUI : 'Bootstrap',
			autowidth : true,
			viewrecords : true,
			pager : jqGrid.pager,
			onInitGrid : jqGrid._onInitGrid,
			loadError : function() {// 加载错误
				layer.msg("加载错误", {
					icon : 5
				});
			}
		}, opt);
		return $(jqGrid.grid).jqGrid(opt);
	},
	initNav : function(opt, inline) {
		if (inline) {
			return jqGrid._initInlineNav();
		}
		// 初始化分页组件参数
		opt = $.extend({
			edit : true,
			add : true,
			del : true,
			search : false,
			refresh : false,
			view : true,
			position : "left",
			cloneToTop : false
		}, opt);
		if (opt.add && opt.addurl) {
			opt.addfunc = function() {
				jqGrid._gotoUrl(opt.addurl, 0);
			}
		}
		if (opt.edit && opt.editurl) {
			opt.editfunc = function(id) {
				jqGrid._gotoUrl(opt.editurl, id);
			}
		}
		if (opt.view && opt.viewurl) {
			opt.viewfunc = function(id) {
				jqGrid._gotoUrl(opt.viewurl, id);
			}
		}
		var editOptions = opt.edit ? jqGrid._getButton(opt.editOptions, opt.beforeSubmit) : null;
		var addOptions = opt.add ? jqGrid._getButton(opt.addOptions, opt.afterSubmit) : null;
		var delOptions = opt.del ? jqGrid._getButton(opt.delOptions, opt.error) : null;
		var viewOptions = opt.view ? $.extend({
			resize : false
		}, opt.viewOptions) : null;
		return $(jqGrid.grid).navGrid(jqGrid.pager, opt, editOptions, addOptions, delOptions, opt.searchOptions || null, viewOptions);
	},
	addNavButton : function(opt) {
		return $(jqGrid.grid).navButtonAdd(jqGrid.pager, opt);
	},
	getPage : function() {
		return $(jqGrid.grid).getGridParam("page");
	},
	_initInlineNav : function() {
		$(jqGrid.grid).inlineNav(jqGrid.pager, {
			edit : true,
			add : true,
			del : true,
			cancel : true
		});
	},
	_onInitGrid : function() { // 初始化表格高度
		var a = $(".content-wrapper").height();
		var b = $(".content-header").outerHeight();
		var c = $(".content").outerHeight();
		var $d = $(".ui-jqgrid-bdiv");
		$d.css("min-height", (a - b - c + $d.height()) + "px");
	},
	_gotoUrl : function(url, id) {
		if (url.indexOf("?") > 0) {
			location.href = url + "&page=" + jqGrid.getPage() + "&id=" + id;
		} else {
			location.href = url + "?page=" + jqGrid.getPage() + "&id=" + id;
		}
	},
	_getButton : function(opt, fun) {// 编辑、添加、删除按钮配置
		return $.extend({
			resize : false,
			closeAfterEdit : true,
			closeAfterAdd : true,
			viewPagerButtons : false,
			beforeSubmit : function() {// 提交前
				jqGrid.index = layer.msg("正在提交...", {
					icon : 16
				});
				if (fun && typeof (fun) == 'function') {
					return [ fun.apply(this, arguments) ];
				}
				return [ true ];
			},
			afterComplete : function(arg) {// 提交成功后
				layer.close(jqGrid.index);
				if (fun && typeof (fun) == 'function') {
					return [ fun.apply(this, arguments) ];
				}
				var result = arg.responseJSON;
				if (result.success) {
					layer.msg(result.msg || "提交成功", {
						icon : 6
					});
					return [ true ];
				} else {
					layer.msg(result.msg || "提交失败", {
						icon : 5
					});
				}
			},
			errorTextFormat : function() {// 提交失败
				layer.close(jqGrid.index);
				layer.msg("提交失败", {
					icon : 5
				});
				if (fun && typeof (fun) == 'function') {
					return fun.apply(this, arguments);
				}
			}
		}, opt);
	}
}